-- 运营活动: 幸运购


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 76, -- 活动id (76 幸运购)
    act_name = "幸运购", -- 活动名称
    act_desc = "每次摇奖都可获取积分，消耗积分兑换奖励！", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1593267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        dailyItems = 
        {
            {type = 0, id = 842, count = 1, bind = 0,},  --第一天团购物品
            {type = 0, id = 852, count = 1, bind = 0,},
            {type = 0, id = 862, count = 1, bind = 0,},
            {type = 0, id = 872, count = 1, bind = 0,},
            {type = 0, id = 1161, count = 1, bind = 0,},
            {type = 0, id = 1162, count = 1, bind = 0,},
            {type = 0, id = 1163, count = 1, bind = 0,},
            {type = 0, id = 1171, count = 1, bind = 0,},
            {type = 0, id = 1172, count = 1, bind = 0,},
            {type = 0, id = 1173, count = 1, bind = 0,},
            {type = 0, id = 1181, count = 1, bind = 0,},
            {type = 0, id = 1182, count = 1, bind = 0,},
            {type = 0, id = 1183, count = 1, bind = 0,},
            {type = 0, id = 1189, count = 1, bind = 0,},
            {type = 0, id = 841, count = 1, bind = 0,},
            {type = 0, id = 851, count = 1, bind = 0,},
            {type = 0, id = 861, count = 1, bind = 0,},
            {type = 0, id = 871, count = 1, bind = 0,},
        },
        dailySpecialItems = 
        {
            {   --第一天特殊奖励
                {type = 0, id = 842, count = 1, bind = 0,},
                {type = 0, id = 852, count = 1, bind = 0,},
                {type = 0, id = 862, count = 1, bind = 0,},
            },
        },
        buyCost = 30, --购买消耗元宝
        AwardTime = 75600, --当天第几秒的时候开奖
        LastBuyCount = 200,     --开奖最低全服购买次数
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_76_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.Awards == nil then return 23 end
    if type(config.Awards) ~= "table" then return 24 end
    for idx = 1, #config.Awards do
        local dayRewards = config.Awards[idx]
        if dayRewards == nil then return 25 end
        if type(dayRewards) ~= "table" then return 26 end
        if dayRewards.awards == nil then return 27 end
        if type(dayRewards.awards) ~= "table" then return 28 end
        if not oper_act_check_one_award_cfg(dayRewards.awards) then
            return 22
        end 
    end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_76_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_76_unsetup(cfg)   
end

-- 活动开始
function oper_act_76_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_76_start = 1
    ssVar.oper_act_76_cmd = cfg.cmd_id
    ssVar.oper_act_76_act = cfg.act_id

    if not cfg then return end
    local config = cfg.config
    if not config then return end

    local lastTime = System.getCurrMiniTime() - System.getToday()

    if lastTime < config.pre_start_time then
        local awardTime = config.pre_start_time - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_pre_start_broadcast")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_pre_start_broadcast", NULL, awardTime, false)
    elseif lastTime < config.pre_start_time + config.next_start_time then
        local awardTime = config.pre_start_time + config.next_start_time - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_give_award", NULL, awardTime, false)
    elseif lastTime < config.pre_start_time + config.next_start_time + config.every_start_time * config.daily_count then
        local awardTime = config.pre_start_time + config.next_start_time + config.every_start_time * config.daily_count - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        awardTime = awardTime % config.every_start_time
        if awardTime == 0 then
            awardTime = config.every_start_time
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_give_award", NULL, awardTime, false)
    end

    local hScene = Fuben.getSceneHandleById(config.senceId, 0)
    Fuben.setSceneAreaAttri(hScene, config.rangeId, aaRunNpc, tostring(config.rangeId), "oper_act_76_enterarea", Actor.getHandle(System.getGlobalNpc()))
end

-- 活动结束
function oper_act_76_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_76_start = nil
    ssVar.oper_act_76_cmd = nil
    ssVar.oper_act_76_act = nil
    ssVar.oper_act_76_daily_count = nil

    if not cfg then return end
    local config = cfg.config
    if not config then return end

    local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_pre_start_broadcast")
    if timerH then
        System.unregisterTimeCall(timerH)
    end

    local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
    if timerH then
        System.unregisterTimeCall(timerH)
    end
end

-- 打包数据
function oper_act_76_packet_data(sysarg, cfg)
    if not cfg then return end
    local config = cfg.config
    if not config then return end
    
end

-- 操作，返回操作结果，0为成功
function oper_act_76_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_76_do cfg.config confg error")
        return
    end
end


-- 跨天
function oper_act_76_new_day_arrive(act_id) 
    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_76_start then
        return
    end
    if not ssVar.oper_act_76_cmd then
        return
    end
    if not ssVar.oper_act_76_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_76_cmd, ssVar.oper_act_76_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end

    ssVar.oper_act_76_daily_count = nil

    local lastTime = System.getCurrMiniTime() - System.getToday()

    if lastTime < config.pre_start_time then
        local awardTime = config.pre_start_time - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_pre_start_broadcast")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_pre_start_broadcast", NULL, awardTime, false)
    elseif lastTime < config.pre_start_time + config.next_start_time then
        local awardTime = config.pre_start_time + config.next_start_time - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_give_award", NULL, awardTime, false)
    elseif lastTime < config.pre_start_time + config.next_start_time + config.every_start_time * config.daily_count then
        local awardTime = config.pre_start_time + config.next_start_time + config.every_start_time * config.daily_count - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        awardTime = awardTime % config.every_start_time
        if awardTime == 0 then
            awardTime = config.every_start_time
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_give_award", NULL, awardTime, false)
    end
    
end

function oper_act_76_pre_start_broadcast()
    local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_pre_start_broadcast")
    if timerH then
        System.unregisterTimeCall(timerH)
    end

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_76_start then
        return
    end
    if not ssVar.oper_act_76_cmd then
        return
    end
    if not ssVar.oper_act_76_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_76_cmd, ssVar.oper_act_76_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end   

    System.broadcastTipmsg(string.format(Lang.ScriptTips.Operation209, enLinkOperationSpringGift),ttScreenCenter + ttChatWindow)

    local lastTime = System.getCurrMiniTime() - System.getToday()
    if lastTime < config.pre_start_time + config.next_start_time then
        local awardTime = config.pre_start_time + config.next_start_time - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_give_award", NULL, awardTime, false)
    end
end

function oper_act_76_give_award()
    local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
    if timerH then
        System.unregisterTimeCall(timerH)
    end

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_76_start then
        return
    end
    if not ssVar.oper_act_76_cmd then
        return
    end
    if not ssVar.oper_act_76_act then
        return
    end
    local cfg = get_oper_act_cfg(ssVar.oper_act_76_cmd, ssVar.oper_act_76_act)
    if not cfg then
        return
    end
    local config = cfg.config
    if not config then return end   

    if not ssVar.oper_act_76_daily_count then
        ssVar.oper_act_76_daily_count = 0
    end

    ssVar.oper_act_76_daily_count = ssVar.oper_act_76_daily_count + 1

    local lastTime = System.getCurrMiniTime() - System.getToday()
    if ssVar.oper_act_76_daily_count >= config.daily_count or 
        lastTime >= config.pre_start_time + config.next_start_time + config.every_start_time * config.daily_count then
        System.broadcastTipmsg(Lang.ScriptTips.Operation213, ttScreenCenter + ttChatWindow)
    else
        local awardTime = config.pre_start_time + config.next_start_time + config.every_start_time * config.daily_count - lastTime
        local timerH = System.getTimeCall(System.getGlobalNpc(), "oper_act_76_give_award")
        if timerH then
            System.unregisterTimeCall(timerH)
        end
        awardTime = awardTime % config.every_start_time
        if awardTime == 0 then
            awardTime = config.every_start_time
        end
        --print("awardTime ="..awardTime)
        System.registerTimeCall(System.getGlobalNpc(), "oper_act_76_give_award", NULL, awardTime, false)
    end

    local myPlayerList = {}
    local playerList = LuaHelp.getSceneActorListById(config.senceId) or {}
    for i,v in ipairs(playerList) do
        local index = Actor.getMapAttriValue(v, aaRunNpc)
        if index == config.rangeId then
            table.insert(myPlayerList, v)
            Actor.playScrEffect( v, enSceneEffectType_MoneyRain, 60)
        end
    end
    local scenePlayer = #myPlayerList
    local needNum = 0
    local needNumTable = {}
    local isAllSendAward = false
    for idx = 1, #config.Awards do
        local myConfig = config.Awards[idx]
        local iii, item = GetItemIdxRand(myConfig.playerCount)
        if item.num ~= -1 then
            needNum = needNum + item.num
        else
            isAllSendAward = true
        end
        --print("iii ="..iii)
        --print("item.num ="..item.num)
        needNumTable[idx] = item.num
    end
    local sendNumTable = {}
    if scenePlayer > 0 then
        local playerSortTable = oper_act_76_get_random_number(1, scenePlayer)
        for idx = 1, scenePlayer do
            local player = myPlayerList[playerSortTable[idx]]
            if player then
                local myAwardIdx = -1
                for ii = 1, #config.Awards do
                    if not sendNumTable[ii] then
                        sendNumTable[ii] = 0
                    end
                    if needNumTable[ii] > sendNumTable[ii] or needNumTable[ii] == -1 then
                        myAwardIdx = ii
                        sendNumTable[ii] = sendNumTable[ii] + 1
                        break
                    end
                end
                if myAwardIdx == -1 then
                    return
                end
                local myConfig = config.Awards[myAwardIdx]
                local awardCount = System.getRandomNumber(myConfig.countRange[1], myConfig.countRange[2])
                local myAwards = GetAwardsByRate( myConfig.awards, 1 )
                myAwards[1].count = awardCount
                local actorid = Actor.getActorId(player)
                local title = string.format(Lang.ScriptTips.Operation210, cfg.act_name)
                local content = string.format(Lang.ScriptTips.Operation211, cfg.act_name, myConfig.desc)
                local sex = Actor.getIntProperty(player, PROP_ACTOR_SEX)
                local vocation = Actor.getIntProperty(player, PROP_ACTOR_VOCATION)
                Awards.GiveToMailById(actorid, myAwards, title, content, LogIds.OperationLogId076, Lang.LogTips.Operation076, vocation, sex)

                local playerName = Actor.getName(player)
                if myConfig.needBroadCast and myConfig.needBroadCast == 1 then
                    System.broadcastTipmsg(string.format(Lang.ScriptTips.Operation212, playerName, awardCount, enLinkOperationSpringGift),ttScreenCenter + ttChatWindow)
                end
            end
        end
    end
end

function oper_act_76_enterarea(sysarg)

end


function oper_act_76_get_random_number(nStart, nEnd)
    local numTable = {}
    local stateTable = {}
    local nLen = nEnd - nStart + 1
    for idx = nStart,  nEnd do
        local num = System.getRandomNumber(nLen) + 1
        local NewNum = num + nStart - 1
        while stateTable[num] do
            num = num + 1
            NewNum = NewNum + 1
            if num > nLen then
                num = num - nLen
                NewNum = NewNum - nLen
            end
        end
        table.insert(numTable, NewNum)
        stateTable[num] = 1
    end
    return numTable
end
